using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace SunshineFarmWord
{
    public class PlayerDigState : PlayerBaseStates
    {
        private GameObject _digNode;
        private SoilDig _soilDig;
        public PlayerDigState(PlayerStateMachine playerStateMachine) : base(playerStateMachine)
        {
            if (stateMachine.ReusableData.PlayerStatesData.PlayerDigData == null) stateMachine.ReusableData.PlayerStatesData.PlayerDigData = new PlayerDigData();
        }

        #region Base Function

        public override void Enter()
        {
            base.Enter();

            _digNode = stateMachine.ReusableData.PlayerStatesData.PlayerDigData.digSoilNode;

            //初始化数据
            InitReusableData();

            //更改角色朝向
            ChangeFaceDirection(_digNode);

            StartAnimation(stateMachine.Player.AnimationData.DigParameterHash);
        }

        public override void Exit()
        {
            base.Exit();

            StopAnimation(stateMachine.Player.AnimationData.DigParameterHash);
        }

        #endregion

        #region Main function
        private void InitReusableData()
        {
            if (_digNode == null) return;
            if (_digNode.TryGetComponent<SoilDig>(out _soilDig))
            {
                _soilDig.InitDigData(stateMachine.ReusableData.PlayerStatesData.PlayerDigData.DigSoilMinNumber, stateMachine.ReusableData.PlayerStatesData.PlayerDigData.ResetDigTime);
            }
        }
        #endregion


        #region 按键监听

        //交互按键
        protected override void OnInteractStarted(InputAction.CallbackContext context)
        {
            base.OnInteractStarted(context);

            //挖掘状态下按交互表示 取消挖掘
            stateMachine.ChangeState(stateMachine.IdlingState);
        }

        #endregion


        #region 动画监听

        // 铲了一次
        public override void OnAnimationTransitionEvent()
        {
            base.OnAnimationTransitionEvent();

            if (_soilDig != null)
            {
                _soilDig.DigDoing();
                if (!_soilDig.IsDigDoing())
                {
                    // 挖掘完毕
                    _soilDig.DigEnd();
                    stateMachine.ReusableData.IsDiggingState = false;
                    stateMachine.ReusableData.PlayerStatesData.PlayerDigData.digSoilNode = null;
                    stateMachine.ChangeState(stateMachine.IdlingState);
                }
            }
        }
        #endregion
    }
}
